/*
 * LogFile.h
 */

#ifndef LOGFILE_H_
#define LOGFILE_H_

#include <exception>
#include <fstream>
#include <string>
#include "../server/Mutex.h"
#include "TimeUtils.h"

class LogFileException : public std::exception {
	public:
		explicit LogFileException(const std::string& msg) throw();
		virtual ~LogFileException() throw() {}
		virtual const char* what() const throw();
	private:
		std::string msg;
};

class LogFile {
private:
	bool outMode;
	std::string accessFileName;
	std::fstream accessFile;
	Mutex accessFile_Mutex;

	std::string errorFileName;
	std::fstream errorFile;
	Mutex errorFile_Mutex;
	void removeIfEmptyFile(std::fstream& file, const std::string& fileName);
	void rewind(std::fstream& archivo);
	bool getLine(std::string& line, std::fstream& archivo);
public:
	LogFile(const std::string& logDir, bool writeMode = false);
	virtual ~LogFile();

	void addAccessInfo(const std::string& uri, int statusCode);
	void addAccessError(const std::string& requestLine, int statusCode);

	void addInfo(const std::string& info);
	void addError(const std::string& error);

	void rewindAccessFile();
	bool getLineAccessFile(std::string& line);
	void rewindErrorFile();
	bool getLineErrorFile(std::string& line);
};

#endif /* LOGFILE_H_ */
